#include "all.h"
#include "interface.h"
#include "FuncPage.h"


struct message
{
    int flag;
    char fromName[20];
    char toName[20];
    char content[1024];
};
typedef struct message Msg;

void* thread_handler(void* arg)
{
    TcpC* c = (TcpC*)arg;
    while(1)
    {
        Msg m;
        TCPClientRecv(c,&m,sizeof(m));
        printf("recv message from [%s] , message:%s\n",m.fromName,m.content);
    }
}

int main(int argc,char* argv[])
{
    TcpC* c;
    user_act* u;
    if(argc != 3)
    {
        printf("invaild numbers!\n");
        return -1;
    }
    
    c = InitTcpClient(argv[1],atoi(argv[2]));
    u = InitUser();

    Menu(u,c);
    printf("success!\n");
    FuncPage(u,c);

    Msg m ;
    scanf("%s",m.fromName);
    while(getchar()!='\n');
    m.flag = 1 ;
    TcpClientSend(c, &m,sizeof(m));
    m.flag = 2 ;

    Thread* t = InitThread(thread_handler,c);
    DetachThread(t);

    while(1)
    {
        printf("please input send name :");
        scanf("%s",m.toName);
        while(getchar()!= '\n');
       
        printf("please input send content :");
        scanf("%[^\n]",m.content);
        while(getchar()!= '\n');

        TcpClientSend(c, &m,sizeof(m));

    }

    return 0;
}